import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.Timer;	//@PEDRO
import java.awt.*;
import java.awt.event.*;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class IncreaseNumMessagesImpl extends java.rmi.server.UnicastRemoteObject implements IncreaseNumMessages{
	private int numMessages;
	private boolean alive=false;
	
	
	Timer timer = new Timer( 5000, new ActionListener(){  		//@PEDRO
	  @Override
	  public void actionPerformed( ActionEvent e ){
		alive=false;
	  }
	} );
		
	
	public IncreaseNumMessagesImpl() throws java.rmi.RemoteException
	{
		timer.setRepeats( true );	//@PEDRO
		timer.start();			//@PEDRO
		
		numMessages = 0;
	}

	public int getNumberOfMessages()  //@PEDRO
	{ 
		return numMessages;
	}

	public boolean getServerAlive()			//@PEDRO
	{
		return alive;
	}
	
	public void setServerAlive(boolean isAlive)			//@PEDRO
	{
		timer.stop();
		timer.start();
		alive = true;
	}
	
	public int increaseNumberOfMessages()
	{
		timer.stop();			//@PEDRO
		timer.start();			//@PEDRO
		alive = true;			//@PEDRO
		 String string="";
	        String file ="numMessages.txt";
	        //int numMessages = 0;
	        //reading   
	        try{
	        	
	        	File f = new File(file);
	        	if(f.exists() && !f.isDirectory()) { 
	        		InputStream ips=new FileInputStream(f); 
	                InputStreamReader ipsr=new InputStreamReader(ips);
	                BufferedReader br=new BufferedReader(ipsr);
	                String line;
	                while ((line=br.readLine())!=null){                
	                    //string+=line+"\n";
	                    break;
	                }
	                numMessages = Integer.parseInt(line);
	                br.close();	
	        	}
	             
	        }       
	        catch (Exception e){
	        	System.out.println("File read exception 0");
	            System.out.println(e.toString());
	            //System.out.println("File read exception 1");
	        }

	        numMessages++;

	        //writing
	        try {
	            FileWriter fw = new FileWriter (file);
	            BufferedWriter bw = new BufferedWriter (fw);
	            PrintWriter fileOut = new PrintWriter (bw); 
	                //fileOut.println (string+"\n test of read and write !!");
	            string= "" + numMessages;
	            System.out.println(string);
	            fileOut.println(string);
	            fileOut.close();
	            //System.out.println("the file " + file + " is created!"); 
	        }
	        catch (Exception e){
	        	System.out.println("File write exception 0");
	            System.out.println(e.toString());
	            System.out.println("File write exception 1");
	        } 
	        return numMessages;
	}
}
